﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TLTuan.App.TransferObj;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace TLTuan.App.AccessLayer
{
    public class CDAOTaiLieu
    {

        #region Interface ...

        public List<CTaiLieu> get_tailieu_byChungloai(int idChungloai)
        {
            DataTable v_dt = CConnection.GetData("pr_tblTailieu_SelectByChungloai", "@FK_Chungloai", idChungloai);
            if (v_dt == null || v_dt.Rows.Count == 0)
                return null;
            else
            {
                List<CTaiLieu> op_lst = new List<CTaiLieu>();
                foreach (DataRow item in v_dt.Rows)
                {
                    op_lst.Add(convertDataRow2Obj(item));
                }

                return op_lst;
            }
        }

        public CTaiLieu get_tailieu_SelectByID(int ip_ID)
        {
            DataTable v_dt = CConnection.GetData("[pr_tblTailieu_SelectOne]", "@PK_TailieuID", ip_ID);
            if (v_dt == null || v_dt.Rows.Count == 0)
                return null;
            else
            {
                return convertDataRow2Obj(v_dt.Rows[0]);
            }
        }

        
        public List<CTaiLieu> get_tailieu_SelecttopByDiem(int ip_top)
        {
            DataTable v_dt = CConnection.GetData("pr_tblTailieu_SelectOrderByDiem");
            if (v_dt == null || v_dt.Rows.Count == 0)
                return null;
            else
            {
                int i = 0;
                List<CTaiLieu> op_lst = new List<CTaiLieu>();
                foreach (DataRow item in v_dt.Rows)
                {
                    op_lst.Add(convertDataRow2Obj(item));
                    i++;
                    if(i == ip_top)break;
                }

                return op_lst;
            }
        }

        public List<CTaiLieu> get_tailieu_byThanhvien(int idThanhvien, int i_pageNum, int i_pageNews)
        {
            DataTable v_dt = CConnection.GetData("pr_tblTailieu_SelectByThanhvien", "@FK_NguoidangID", idThanhvien, "@PageNum", i_pageNum, "@PageNews", i_pageNews);
            if (v_dt == null || v_dt.Rows.Count == 0)
                return null;
            else
            {
                List<CTaiLieu> op_lst = new List<CTaiLieu>();
                foreach (DataRow item in v_dt.Rows)
                {
                    op_lst.Add(convertDataRow2Obj(item));
                }

                return op_lst;
            }
        }

        #endregion

        #region Method
        internal object get_all_news()
        {
            return CConnection.GetData("pr_tblTailieu_SelectAll");
        }

        public bool Insert(CTaiLieu ip_obj)
        {
            return (CConnection.ExecuteNonQuery("[pr_tblTaiLieu_Insert]",
                "@PK_TailieuID", ip_obj.PK_TailieuID,
                "@MaTaiLieu",ip_obj.MaTaiLIeu,
                "@TenTaiLieu",ip_obj.TenTaiLieu,
                "@FK_NguoidangID",ip_obj.FK_NguoidangID,
                "@Mota",ip_obj.Mota,
                "@Link",ip_obj.Link,
                "@Dungluong",ip_obj.Dungluong,
                "@Diem",ip_obj.diem,
                "@Ngaydang",ip_obj.Ngaydang,
                "@FK_ChungloaiID",ip_obj.FK_ChungloaiID,
                "@FK_NhomTLID",ip_obj.FK_NhomTLID) > 0);
        }

        internal bool Delete(int ip_Id)
        {
            SqlConnection v_conn = CConnection.GetConnection();

            SqlCommand v_comm = new SqlCommand("[pr_tblTailieu_Delete]", v_conn);
            v_comm.CommandType = CommandType.StoredProcedure;
            v_comm.Parameters.Add("@PK_TailieuID", SqlDbType.Int).Value = ip_Id;
            int v_row = v_comm.ExecuteNonQuery();

            // Close connection
            CConnection.Close();

            if (v_row > -1)
                return true;
            else
                return false;
        }

        internal DataTable get_all_news_by_search(string p, int p_2, string p_3)
        {
            throw new NotImplementedException();
        }
        
        private CTaiLieu convertDataRow2Obj(DataRow ip_dr)
        {
            // Khoi tao
            CTaiLieu op_tailieu = new CTaiLieu();

            // Gan cac thuoc tinh
            op_tailieu.PK_TailieuID = int.Parse(ip_dr["PK_TailieuID"].ToString());
            op_tailieu.MaTaiLIeu = ip_dr["MaTaiLieu"].ToString();
            op_tailieu.TenTaiLieu = ip_dr["TenTaiLieu"].ToString();
            op_tailieu.FK_NguoidangID = int.Parse(ip_dr["FK_NguoidangID"].ToString());
            op_tailieu.Mota = ip_dr["Mota"].ToString();
            op_tailieu.Link = ip_dr["Link"].ToString();
            op_tailieu.Dungluong = int.Parse(ip_dr["Dungluong"].ToString());
            op_tailieu.diem = int.Parse(ip_dr["Diem"].ToString());
            op_tailieu.Ngaydang = (DateTime)ip_dr["Ngaydang"];
            op_tailieu.FK_ChungloaiID = int.Parse(ip_dr["FK_ChungloaiID"].ToString());
            op_tailieu.FK_NhomTLID = int.Parse(ip_dr["FK_NhomTLID"].ToString());

            return op_tailieu;
        }


        #endregion
    }
}
